﻿using System;
using UnityEngine;

namespace RootMotion.FinalIK
{
	// Token: 0x0200018B RID: 395
	public class TwistRelaxer : MonoBehaviour
	{
		// Token: 0x060007E8 RID: 2024 RVA: 0x0002B110 File Offset: 0x00029510
		public void Relax()
		{
			if (this.weight <= 0f)
			{
				return;
			}
			Vector3 a = this.parent.rotation * this.axisRelativeToParentDefault;
			Vector3 b = this.child.rotation * this.axisRelativeToChildDefault;
			Vector3 point = Vector3.Slerp(a, b, this.parentChildCrossfade);
			Quaternion rotation = Quaternion.LookRotation(base.transform.rotation * this.axis, base.transform.rotation * this.twistAxis);
			point = Quaternion.Inverse(rotation) * point;
			float num = Mathf.Atan2(point.x, point.z) * 57.29578f;
			Quaternion rotation2 = this.child.rotation;
			base.transform.rotation = Quaternion.AngleAxis(num * this.weight, base.transform.rotation * this.twistAxis) * base.transform.rotation;
			this.child.rotation = rotation2;
		}

		// Token: 0x060007E9 RID: 2025 RVA: 0x0002B220 File Offset: 0x00029620
		private void Start()
		{
			Vector3 point = base.transform.rotation * this.axis;
			this.axisRelativeToParentDefault = Quaternion.Inverse(this.parent.rotation) * point;
			this.axisRelativeToChildDefault = Quaternion.Inverse(this.child.rotation) * point;
		}

		// Token: 0x060007EA RID: 2026 RVA: 0x0002B27C File Offset: 0x0002967C
		private void LateUpdate()
		{
			this.Relax();
		}

		// Token: 0x040004CB RID: 1227
		[Tooltip("The weight of relaxing the twist of this Transform")]
		[Range(0f, 1f)]
		public float weight = 1f;

		// Token: 0x040004CC RID: 1228
		[Tooltip("If 0.5, this Transform will be twisted half way from parent to child. If 1, the twist angle will be locked to the child and will rotate with along with it.")]
		[Range(0f, 1f)]
		public float parentChildCrossfade = 0.5f;

		// Token: 0x040004CD RID: 1229
		[Tooltip("The parent Transform, does not need to be the actual transform.parent.")]
		public Transform parent;

		// Token: 0x040004CE RID: 1230
		[Tooltip("The child Transform, does not need to be the direct child, you can skip bones in the hierarchy.")]
		public Transform child;

		// Token: 0x040004CF RID: 1231
		[Tooltip("The local axis of this Transform that it will be twisted around (the axis pointing towards the parent).")]
		public Vector3 twistAxis = Vector3.right;

		// Token: 0x040004D0 RID: 1232
		[Tooltip("Another axis, orthogonal to twistAxis.")]
		public Vector3 axis = Vector3.forward;

		// Token: 0x040004D1 RID: 1233
		private Vector3 axisRelativeToParentDefault;

		// Token: 0x040004D2 RID: 1234
		private Vector3 axisRelativeToChildDefault;
	}
}
